//
//  Utilities.m
//  CategoryDemo
//
//  Created by Pham Vu Duong on 5/22/12.
//  Copyright (c) 2012 CMC Software Solution Company Ltd. All rights reserved.
//

#import "Utilities.h"
#import "UIImage+CMCUtilities.h"
#import <CommonCrypto/CommonDigest.h>
#import "AppDelegate.h"

#define appDelegate (AppDelegate*)[[UIApplication sharedApplication] delegate]

#define MEME_TEXT_RATIO 10/100
#define FONT_NAME @"Impact"
#define FONT_SIZE 20

@interface Utilities (internal)
CGFloat DegreesToRadians(CGFloat degrees);
CGFloat RadiansToDegrees(CGFloat radians);
@end

@implementation Utilities

+ (NSString*)MD5Encodine:(NSString*)inputString
{
    
    
    const char *cStr = [inputString UTF8String];
    unsigned char digest[16];
    CC_MD5( cStr, strlen(cStr), digest ); // This is the md5 call
    
    NSMutableString *output = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
    
    for(int i = 0; i < CC_MD5_DIGEST_LENGTH; i++)
        [output appendFormat:@"%02x", digest[i]];
    return output;
}

+ (NSString*)Base64Encoding:(NSString*)inputString
{
    return inputString;
}


+(NSString*)uniqueStringWithPrefix:(NSString*)prefix{
    
    NSDate *now=[NSDate date];
    NSDateFormatter *dFormat=[[NSDateFormatter alloc] init];
    [dFormat setDateFormat:@"yyyyMMddHHmmss"];
    return [NSString stringWithFormat:@"%@%@",prefix,[dFormat stringFromDate:now]];
    
}

+(void)getASyncUIImage:(NSString*)path
               success:(void (^)(UIImage* image))success
               failure:(void (^)(NSError *error))failure{
    UIImage *image=[UIImage imageWithContentsOfFile:path];
    if (image!=nil) {
        if (success) {
            success(image);
        }
    }
    else if (failure) {
        failure([NSError errorWithDomain:@"CMCSoftware" code:500 userInfo:[NSDictionary dictionaryWithObject:@"Cannot load image" forKey:NSLocalizedDescriptionKey]]);
    }

}
/*
#define SECOND 1
#define MINUTE SECOND * 60
#define HOUR MINUTE * 60
#define DAY HOUR * 24
#define WEEK DAY * 7
#define MONTH DAY * 30
#define YEAR DAY *365
*/


+(NSString *)getComparisionNowTimeWithDate:(NSString *)date
{
    int SECOND = 1;
    int MINUTE = SECOND * 60;
    int HOUR = MINUTE * 60;
    int DAY = HOUR * 24;
    int WEEK = DAY * 7;
    int MONTH = DAY * 30;
    int YEAR = DAY *365;
    
    //time interval
//    NSLog(@"%d|%d|%d|%d|%d",SECOND,MINUTE,HOUR,DAY,WEEK);
//    NSLog(@"%@",date);
    int x = [[NSDate date] timeIntervalSince1970] - date.intValue - 5*60*60;
    if (x>YEAR) {
        if (x%YEAR==2) {
            return @"about 1 year ago";
        }
        else{
            return [NSString stringWithFormat:@"about %d years ago",x/YEAR];
        }
    }
    else if (x>MONTH) {
        if (x%MONTH==2) {
            return @"about 1 month ago";
        }
        else{
            return [NSString stringWithFormat:@"about %d months ago",x/MONTH];
        }
    }
    else if (x>WEEK) {
        if (x%WEEK==2) {
            return @"about 1 week ago";
        }
        else{
            return [NSString stringWithFormat:@"about %d weeks ago",(x%MONTH)/WEEK];
        }
    }
    else if (x>DAY) {
        if (x%DAY==2) {
            return [NSString stringWithFormat:@"%d day and %d hours ago",(x%WEEK)/DAY,(x%DAY)/HOUR];
        }
        else{
            return [NSString stringWithFormat:@"%d days and %d hours ago",(x%WEEK)/DAY,(x%DAY)/HOUR];
        }
    }
    else if (x>HOUR) {
//        NSLog(@"%d,%d",DAY,HOUR);
//        NSLog(@"%@",[NSString stringWithFormat:@"%i hours and %i minute ago",(int)((x%DAY)/HOUR),(int)((x%HOUR)/MINUTE)]);
        return [NSString stringWithFormat:@"%d hours and %d minute ago",(x%DAY)/HOUR,(x%HOUR)/MINUTE];
    }
    else if (x>MINUTE){
        return [NSString stringWithFormat:@"%d minute and %d second ago",(x%HOUR)/MINUTE,x%MINUTE];
    }
    else {
        return [NSString stringWithFormat:@"%d second ago",x%MINUTE];
    }
        
    
    
    return @"";

}

+(DateStringCompare)compareDate:(NSString*)leftDateString withDate:(NSString*)rightDateString{
    
    return 0;
}

@end
